package com.zhongshengnetwork.rightbe.https;

/**
 * Created by luojingxing on 2018/2/15.
 */
import com.android.volley.AuthFailureError;
        import com.android.volley.Response;
        import com.android.volley.VolleyLog;
        import com.android.volley.toolbox.JsonObjectRequest;

        import org.json.JSONException;
        import org.json.JSONObject;

        import java.io.UnsupportedEncodingException;
        import java.util.Iterator;
        import java.util.LinkedHashMap;
        import java.util.Map;

/**
 * 解决Volley中的JsonObjectRequest jsonRequest参数无法被服务端读取的问题
 * <br>
 *     服务端：SpringBoot
 *     追溯到父类方法，发现Volley只是将 jsonRequest.toString().getBytes()作为request body发送到服务端，导致服务端无法识别
 *
 */
public class CustomJsonObjectRequest extends JsonObjectRequest {

    private JSONObject mRequestBody;

    /*
    public CustomJsonObjectRequest(int method, String url, JSONObject jsonRequest, Response.Listener<JSONObject> listener) {
        super(method, url, jsonRequest, listener, new MyRequestErrorListener(url));

        this.mRequestBody = jsonRequest;
    }

    public CustomJsonObjectRequest(String url, JSONObject jsonRequest, Response.Listener<JSONObject> listener) {
        super(url, jsonRequest, listener, new MyRequestErrorListener(url));

        this.mRequestBody = jsonRequest;
    }
    */

    public CustomJsonObjectRequest(int method, String url, JSONObject jsonRequest, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
        super(method, url, jsonRequest, listener, errorListener);
        this.mRequestBody = jsonRequest;
    }

    public CustomJsonObjectRequest(String url, JSONObject jsonRequest, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
        super(url, jsonRequest, listener, errorListener);
        this.mRequestBody = jsonRequest;
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String, String> headers = new LinkedHashMap<>();
        //让服务器识别request参数在request body中
        headers.put("Content-Type", "application/x-www-form-urlencoded");
        return headers;
    }

    @Override
    public byte[] getBody() {
        StringBuilder requestBodySb = new StringBuilder();

        //遍历JSONObject中的k-v
        Iterator<String> keys = this.mRequestBody.keys();
        try {
            boolean hasNext = keys.hasNext();
            while (hasNext) {

                String key = keys.next();
                String value = mRequestBody.get(key).toString();
                requestBodySb.append(key).append("=").append(value);

                hasNext = keys.hasNext();
                if (hasNext) {
                    requestBodySb.append("&");
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        // to bytes
        try {
            String bytes = requestBodySb.toString();
            return bytes.getBytes(PROTOCOL_CHARSET);
        } catch (UnsupportedEncodingException e) {
            VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
                    mRequestBody, PROTOCOL_CHARSET);
        }
        return null;
    }


}